package com.wanhe.mealbookmodule.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.wanhe.mealbookmodule.adapter.views.IListViewAdapter;
import com.wanhe.mealbookmodule.adapter.views.OnDataOperationResultListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cdj on 2017/6/22.
 */

public class CommonAdapter<M> extends BaseAdapter implements IListViewAdapter<M> {

	protected final int						CUSTOM_ITEM_FLAG	= 0;
	protected final Context					mContext;

	private List<M>							mList				= new ArrayList<>();
	private OnDataOperationResultListener mListener;

	public CommonAdapter(Context context) {
		this.mContext = context;
	}

	public void setOnDataOperationResultListener(OnDataOperationResultListener listener) {
		this.mListener = listener;
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	/**
	 * 用于获取单项
	 * 
	 * @author 陈德基
	 * @time 2017/6/22 15:33
	 */
	public M getItemAtPosition(int position) {
		return (M) getItem(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public void loadMore(List<M> list) {
		if (list != null && list.size() > 0) {
			this.mList.addAll(list);
			notifyDataSetChanged();
			if (mListener != null) {
				mListener.listLoaded(this);
			}
		} else {
			if (mListener != null) {
				mListener.noMoreData();
			}
		}
	}

	@Override
	public void refresh(List<M> list) {
		if (list != null && list.size() > 0) {
			this.mList.clear();
			this.mList.addAll(list);
			notifyDataSetChanged();
			if (mListener != null) {
				mListener.listLoaded(this);
			}
		} else {
			if (mListener != null) {
				mListener.empty();
			}
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		int layoutID = getLayoutID();
		if (layoutID != CUSTOM_ITEM_FLAG) {
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(layoutID, null, false);
				holder = createHolder();
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			bindViewHolder(holder, position);
		} else {
			if (convertView == null) {
				convertView = createCustomerView(position);
			} else {
				bindViewData(convertView, position);
			}
		}
		return convertView;
	}

	protected void bindViewData(View convertView, int position) {
	}

	/**
	 * 生成自定义view
	 * 
	 * @author 陈德基
	 * @time 2017/6/22 16:06
	 */
	protected View createCustomerView(int position) {
		return null;
	}

	/**
	 *
	 * 如果没有使用自定义控件需要绑定holder
	 *
	 * @author 陈德基
	 * @time 2017/6/22 16:03
	 */
	protected void bindViewHolder(ViewHolder holder, int position) {
	}

	protected ViewHolder createHolder() {
		return new ViewHolder();
	}

	protected int getLayoutID() {
		return CUSTOM_ITEM_FLAG;
	}

	public class ViewHolder {}

}
